package com.github.interceptor;

import com.github.util.JwtUtil;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class AuthInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = (String) request.getSession().getAttribute("token");
        String username = (String) request.getSession().getAttribute("username");
        if (token == null || username == null) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
//            response.sendRedirect("/DbLgoin2025_Ab7cD2Ef9G.html");
            return false;
        }
        if (JwtUtil.validateToken(token, username)) {
            return true;
        }
        response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
//        response.sendRedirect("/DbLgoin2025_Ab7cD2Ef9G.html");
        return false;
    }
}